package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断正准备加入到当前购物车里的商品是否原本已经存在了
        ShoppingCart shoppingCart = new ShoppingCart();//来获取我们需要的userId
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);//赋值userId
        log.info(shoppingCart.getUserId()+"+++++++++++++++++++++++++++++++++++++++");
        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
        if(list!=null&&list.size()>0) {//说明表中存在相同的菜，直接加一就可以
            //如果存在，直接更新数据库数据，number加一即可
            //从数据库为空开始的每一次添加数据，如果菜品重复都是在原有的基础上加一，而不是新增一个菜品到购物车
            //所以查询的数据一定只有一条
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }else {
            //如果不存在，直接插入当前数据到购物车数据库中
            //判断当前的数据是菜品还是套餐
            //根据shoppingcart类中的dishid去查dish表
            Dish dish = dishMapper.selectById(shoppingCart.getDishId());
            if (shoppingCart.getDishId() != 0) {
                //说明当前数据是菜品
                //我们要插入当前的菜品和创建时间以及数量+1
                log.info(shoppingCart.getUserId() + "------------------------------");
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                //涉及到菜品可能同名但是口味不一样
                // 所以我们是直接添加新的菜品，而不是在旧的菜品上加一
                //shoppingCart.setNumber(shoppingCart.getNumber()+1);//逻辑不对，但是可以这么写，因为本身number就是0
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
//            shoppingCart.setDishFlavor();//已经在shoppingcart中封装过了
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            } else {
                //那就是套餐了
                //一开始shoppingcart里面封装的只有userId和setmealId
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }
        }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        List<ShoppingCart>list=shoppingCartMapper.select(currentId);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void delete(){
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.delete(currentId);
    }

//    @Override
//    public void deleteById(ShoppingCartDTO shoppingCartDTO) {
//
//    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
/*    @Override
    public void deleteById(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();
        if(shoppingCartDTO.getDishId()!=0){
            //那说明是菜品
            shoppingCartMapper.deleteById(shoppingCartDTO,currentId);
        }else{
            //说明是套餐
            shoppingCartMapper.deleteById(shoppingCartDTO,currentId);
        }

    }*/


    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if(list != null && list.size() > 0){
            shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
